<?php
namespace cms\modules\v1\models\userFund;

use cms\modules\v1\models\user\User;
use common\models\Functions;
use common\models\Status;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class RechargeSetting extends \common\models\userFund\RechargeSetting
{


    public function rules()
    {
        return [
            #添加
            [['name','money',],  'required', 'message' => '{attribute}不能空', 'on' => 'addEdit'],
            ['name', 'string','min' => 0,'max' => 100,"tooLong"=>"限制100字", "tooShort"=>"不能为空",'on' => 'addEdit'],

        ];
    }

    public function scenarios()
    {
        $newScenarios =  [
            'addEdit' => [
                'name',
                'money',
                'ios_id',
            ],
        ];
        return array_merge(parent::scenarios(), $newScenarios);
    }

    /**
     * 起别名
     */
    public function attributeLabels()
    {
        return [
            'name' => '名称',
            'money' => '价格',
            'ios_id' => '内购id',
        ];
    }

    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getListData($page = 1, $size = 20) {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件

            //得到文章的总数
            $count = self::getCountByAndWhere($searchWhere['where'], $searchWhere['andWhere']);
            $data = static::setPageinationAPI($page, $size, $count);
            $offset = ($page - 1) * $size;

            //排序 order
            $orderSql = 'id DESC';

            $list = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);

            $data['data'] = $this->handelList($list);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @return array
     */
    public function getSearch () {
        $where = [];
        $andWhere = [];
        $status = Yii::$app->request->get('status',0);

        if($status) {
            $where['status'] = (int) $status;
        }

        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 处理初始化数据
     * @param $query
     * @return mixed
     */
    public function handelList($query) {
        if (empty($query)) {
            return null;
        }

        $list = $query->with([


        ])->asArray()->all();

        foreach($list as &$value) {

        }
        return $list;
    }

    /**
     * 获取修改信息
     * @param int $id
     * @return array
     */
    public function getInfo($id = 0) {
        try {
            if (empty($id)) {
                throw new Exception('未知信息');
            }

            $info = static::getOneInfoById($id);
            if (empty($info)) {
                throw new Exception('未知信息');
            }

            $data = [
                'id' => (int) $id,
                'name' => $info['name'],
                'money' => floatval($info['money']),
                'ios_id' => $info['ios_id'],
            ];

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 添加修改
     * @return array
     */
    public function addEdit() {
        $trans = Yii::$app->db->beginTransaction();
        try {
            if (!$this->validate()) {
                throw new Exception($this->getErrorSummary(true)[0]);
            }


            $post = Yii::$app->request->post();
            $id = isset($post['id']) ? $post['id'] : 0;
            $data = [];
            $data = array_merge($data, $this->getSaveData($this->scenario, $post));
            if (!empty($id)) {
                //更新操作
                $data['update_time'] = date('Y-m-d H:i:s');
                $res = static::updateDataWithLog($data, ['id' => $id]);
                if ($res === false) {
                    throw new Exception('更新失败');
                }
            } else {
                //添加操作
                $data['update_time'] = $data['create_time'] = date('Y-m-d H:i:s');

                $res = static::insertDataWithLog($data);
                if ($res === false) {
                    throw new Exception('添加失败');
                }

            }
            $trans->commit();
            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            $trans->rollBack();
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }



}
